
import org.groovyflow.db.*
import com.bus.*

class ExptController {
	
	def hey(req, res){
	    def rec = new Product()
	    //If we commented this in, then all would take place in a singe transaction rather than
	    //in many separate transactions.
		//DbHelp.inTrx() {
			def idAsString = "1"
		    rec = rec.findById(idAsString)
			assert 'Lamp' == rec.description
			assert new BigDecimal(idAsString) ==  rec.id
			
			def oldPrice = rec.price
			
			def priceChange = new PriceChange()
			priceChange.increase = 3
			
			rec.changePrice(priceChange)
			assert oldPrice + 3 == rec.price
			
			assert priceChange.id != null
			assert oldPrice == priceChange.oldPrice
			
			rec.save()
			//Just checking that we didn't change the id when we saved.
			assert new BigDecimal(idAsString) ==  rec.id
			
			println 'finished update'
			
			def toInsert = new Product()
			
			assert null == toInsert.id
			
			toInsert.price = 0.03
			toInsert.description = 'Wow'
			toInsert.categoryid = 2
			
			assert toInsert.price == 0.03
			assert 'Wow' == toInsert.description
			assert 2 == toInsert.categoryId
			
			toInsert.save()
			
			//Newly inserted record should have id
			assert toInsert.id != null		
			
			toInsert.delete()
			
			//Id field should have been nulled upon delete.
			assert null == toInsert.id 
			
			//But other fields should retain their values
			assert toInsert.price == 0.03
			assert 'Wow' == toInsert.description
			assert 2 == toInsert.categoryId
			
			//Test join fields (and also that id's can be found by number, not just by String
			def hasJoinFields = new Product().findWithCategoryInfo(new BigDecimal(idAsString))
			assert "Furniture" == hasJoinFields.categoryname
			
			
			//Testing extra methods.  Note that these methods will actually come to exist
			//on every Product.  The values are stored in the extValues Map of the
			//ActiveRecord, along with any join fields
			hasJoinFields.extraCalls << "anEXTRAMethod"
			hasJoinFields.anExtraMethod = "value"
			assert hasJoinFields.anExtraMethod == "value"
			
			//Testing sValues
			def map = [:]
			map.price = 55
			map.description = 'x'
			map.extraField = 'yy'
			def aProd = new Product()
			aProd.sValues(map)
			assert map.price == aProd.price
			assert map.description == aProd.description
			assert map.extraField == aProd.extraField
			
			
			//Let's work with method-style syntax
			assert aProd.price == aProd.getPrice()
			assert aProd.description == aProd.getDescription()			
			assert aProd.extraField == aProd.getExtraField()
			
			aProd.setPrice(5)
			assert 5 == aProd.price
			
			//Do it again just to check that our dynamic creation of methods/properties doesn't
			//mess things up on a second call.
		    assert aProd.price == aProd.getPrice()
			assert aProd.description == aProd.getDescription()			
			assert aProd.extraField == aProd.getExtraField()
			
			aProd.setPrice(5)
			assert 5 == aProd.price
			
				
		 //}

		
		return null
	}
	
	
}

